<template>
	<el-card shadow="never" header="个人信息">
		<el-form ref="form" :model="form" :rules="rules" label-width="120px" style="margin-top:20px;">
			<el-form-item label="头像" prop="avatar">
				<sc-upload
					v-model="form.avatar"
					:apiObj="apiObj"
					title="上传头像"
				></sc-upload>
			</el-form-item>
			<el-form-item label="登陆账号">
				<span style="font-weight: 600;font-size: 16px">{{ this.$TOOL.data.get('USER_INFO').accountId }}</span>
				<div class="el-form-item-msg">账号信息用于登录，系统不允许修改</div>
			</el-form-item>
			<el-form-item label="姓名" prop="userName">
				<el-input v-model="form.userName" style="width: 200px" maxlength="20"></el-input>
			</el-form-item>
			<el-form-item label="登录别名" prop="alias">
				<el-input v-model="form.alias" placeholder="用于登录系统" style="width: 200px" clearable></el-input>
			</el-form-item>
			<el-form-item label="用户昵称" prop="byName">
				<el-input v-model="form.byName" placeholder="用户昵称" style="width: 200px" clearable></el-input>
			</el-form-item>
			<el-form-item label="性别">
				<el-select v-model="form.sex" placeholder="请选择" style="width: 200px">
					<el-option label="男" value="1"></el-option>
					<el-option label="女" value="2"></el-option>
					<el-option label="保密" value="0"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="个性签名">
				<el-input v-model="form.sign" type="textarea" placeholder="个性签名" style="width: 200px"></el-input>
			</el-form-item>
			<el-form-item label="出生日期">
				<el-date-picker type="date" v-model="form.birthday" style="width: 200px" placeholder="出生日期" value-format="YYYY-MM-DD"></el-date-picker>
			</el-form-item>
			<el-form-item label="电子邮件" prop="email">
				<el-input v-model="form.email" type="text" size="50" style="width: 200px" placeholder="电子邮件"></el-input>
			</el-form-item>
			<el-form-item label="手机号码" prop="phone">
				<el-input v-model="form.phone" type="text" size="50" style="width: 200px" placeholder="手机号"></el-input>
			</el-form-item>
			<el-form-item label="座机号码" prop="tel">
				<el-input v-model="form.tel" type="text" size="20" style="width: 200px" placeholder="手机号"></el-input>
			</el-form-item>
			<el-form-item label="所在城市" prop="cityCode">
				<el-select v-model="form.cityCode" clearable filterable remote :remote-method="remoteMethod" placeholder="请输入城市名称" style="width: 200px">
					<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
				</el-select>
				<div class="el-form-item-msg">为空时默认为公网IP地址进行定位，内网IP则无效。内网IP时请先设置所在城市，用于获取天气信息。</div>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="updateUserInfo">保存</el-button>
			</el-form-item>
		</el-form>
	</el-card>
</template>

<script>
import {verifyPhone, verifyEmail} from "@/utils/verificate";

export default {
	data() {
		return {
			apiObj: this.$API.file.uploadAvatar,
			form: {
				avatar: "",
				userName: "",
				byName:"",
				alias:"",
				sex: "",
				sign: "",
				email: "",
				phone: "",
				birthday: "",
				tel: "",
				cityCode:""
			},
			options: [],
			rules: {
				userName: [
					{required: true, message: '请输入真实姓名'}
				],
				email: [
					{validator: verifyEmail, trigger: 'blur'}
				],
				phone: [
					{validator: verifyPhone, trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
		this.remoteMethod("");
		var userInfo = this.$TOOL.data.get("USER_INFO");
		this.form.avatar = userInfo.avatar;
		this.form.alias = userInfo.alias;
		this.form.userName = userInfo.userName;
		this.form.byName = userInfo.byName;
		this.form.sex = userInfo.sex;
		this.form.sign = userInfo.sign;
		this.form.email = userInfo.email;
		this.form.phone = userInfo.phone;
		this.form.birthday = userInfo.birthday;
		this.form.tel = userInfo.tel;
		this.form.cityCode = userInfo.cityCode;
	},
	methods: {
		async remoteMethod(query){
			let res = await this.$API.system.getCityCodeList.get({keyword:query});
			if (res.code == 200) {
				this.options = res.data
			}
		},
		async updateUserInfo() {
			this.$refs.form.validate(async (valid) => {
				if (valid) {
					var res = await this.$API.system.user.update.post(this.form);
					if (res.code == 200) {
						var userInfo = this.$TOOL.data.get("USER_INFO");
						userInfo.userName = this.form.userName;
						userInfo.alias = this.form.alias;
						userInfo.byName = this.form.byName;
						userInfo.sex = this.form.sex;
						userInfo.sign = this.form.sign;
						userInfo.email = this.form.email;
						userInfo.phone = this.form.phone;
						userInfo.birthday = this.form.birthday;
						userInfo.tel = this.form.tel;
						userInfo.avatar = this.form.avatar;
						userInfo.cityCode = this.form.cityCode;
						this.$TOOL.data.set("USER_INFO", userInfo);
						this.$message.success(res.message)
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		}
	}
}
</script>

<style>
</style>
